package Admin;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFormattedTextField;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Vector;
import java.awt.Font;
import java.awt.Color;

public class DesignerEdit extends JPanel {
	JPanel contentPane;
	MemberVo v;
	ManageDTO dto;
	private JLabel lblNewLabel;
	private JLabel label;
	private JLabel label_1;
	private JLabel mid;
	private JLabel irum;
	private JLabel grade;
	private JScrollPane scrollPane;
	private JTable table;
	private JButton btnNewButton;
	private JButton button;
	/**
	 * Create the panel.
	 */
	public DesignerEdit(JPanel contentPane ,MemberVo v) {
		setBackground(Color.WHITE);
		this.contentPane=contentPane;
		this.v=v;
		setLayout(null);
		add(getLblNewLabel());
		add(getLabel());
		add(getLabel_1());
		add(getMid());
		add(getIrum());
		add(getGrade());
		add(getScrollPane());
		add(getBtnNewButton());
		add(getButton());
		
		
		dto = new ManageDTO();
		mid.setText(v.getMid());
		irum.setText(v.getIrum());
		grade.setText(v.getGrade());
		
		Vector<String> vec = new Vector<String>();
		vec.add("Code");
		vec.add("헤어스타일");
		vec.add("category");
		vec.add("가격");
		DefaultTableModel tmodel = new DefaultTableModel(vec,0);
		
		ArrayList<PriceVo> al = dto.price(v.getMid());
		

		for(PriceVo vo : al){
			Vector ve = new Vector();
			
			ve.add(vo.getCode());
			ve.add(vo.getItem());
			ve.add(vo.getCategory());
			ve.add(vo.getCharge());
				
			tmodel.addRow(ve);
		}
		
		table.setModel(tmodel);
		
		
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("ID : ");
			lblNewLabel.setFont(new Font("Gulim", Font.PLAIN, 12));
			lblNewLabel.setBounds(276, 63, 40, 23);
		}
		return lblNewLabel;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("이름 : ");
			label.setFont(new Font("Gulim", Font.PLAIN, 12));
			label.setBounds(276, 92, 40, 23);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("직급 : ");
			label_1.setFont(new Font("Gulim", Font.PLAIN, 12));
			label_1.setBounds(276, 121, 40, 23);
		}
		return label_1;
	}
	private JLabel getMid() {
		if (mid == null) {
			mid = new JLabel("");
			mid.setFont(new Font("Gulim", Font.PLAIN, 12));
			mid.setBounds(322, 63, 77, 23);
		}
		return mid;
	}
	private JLabel getIrum() {
		if (irum == null) {
			irum = new JLabel("");
			irum.setFont(new Font("Gulim", Font.PLAIN, 12));
			irum.setBounds(322, 92, 77, 23);
		}
		return irum;
	}
	private JLabel getGrade() {
		if (grade == null) {
			grade = new JLabel("");
			grade.setFont(new Font("Gulim", Font.PLAIN, 12));
			grade.setBounds(322, 121, 77, 23);
		}
		return grade;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(276, 150, 342, 358);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent arg0) {
					
				}
			});
		}
		return table;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("저장");
			btnNewButton.setBackground(Color.WHITE);
			btnNewButton.setFont(new Font("Gulim", Font.PLAIN, 12));
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					dto = new ManageDTO();
					
					boolean flag = true;
					
					for(int a = 0; a<table.getRowCount();a++){
						PriceVo v = new PriceVo();
						v.setMid(mid.getText());
						v.setCode((String)table.getValueAt(a, 0));
						v.setCharge((String)table.getValueAt(a, 3));
						
						if(!dto.chkinup(v)){
							flag = false;
							break;						
						}
					}
					if(flag){
						JOptionPane.showMessageDialog(DesignerEdit.this, "저장 완료");
						
					}else{
						JOptionPane.showMessageDialog(DesignerEdit.this, "저장 중 오류 발생");
					}
					
					dto.closeconn();
				}
			});
			btnNewButton.setBounds(532, 518, 86, 23);
		}
		return btnNewButton;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("돌아가기");
			button.setBackground(Color.WHITE);
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					contentPane.removeAll();
					contentPane.add(new MemberPanel(contentPane));
					contentPane.updateUI();					
				}
			});
			button.setFont(new Font("Gulim", Font.PLAIN, 12));
			button.setBounds(532, 551, 86, 23);
		}
		return button;
	}
}
